﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses.Internal;

namespace Project_Udrinkwater.Customer
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private static string strcon = ConfigurationManager.ConnectionStrings["DB_UdrinkConnectionString"].ToString();
        protected void Button1_Click(object sender, EventArgs e)
        {

            bool chk = CheckID(txtUser.Text);
            if (chk == true)
            {
                Label1.Text = "ใช้ไม่ได้";
                Label1.Enabled = false;
            }
            else
            {
                Label1.Text = "ใช้ได้";
                Label1.Enabled = true;
            }


        }



        private bool CheckID(string Usertext)
        {
            bool chk = false;
            if (Usertext == "")
            {
                return true;
            }
            else
            {
                string sqltext = @"SELECT Customer_ID FROM TB_Customer WHERE Customer_ID = '" + Usertext + "'";
                SqlConnection conn = new SqlConnection(strcon);
                SqlDataAdapter dap = new SqlDataAdapter(sqltext, conn);
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                dap.Fill(ds);
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    chk = true;
                }
                else
                {
                    chk = false;
                }
                return chk;
            }

        }



        public static object GetIsNull(object value)
        {
            if (value == null || value == "")
            {
                return DBNull.Value;
            }

            return value;
        }

        public void InsertCustomer()
        {
            string sqltext = @"  insert into TB_Customer 
		                (Customer_ID
		                ,E_mail
		                ,[Prefix]
                         ,[Firstname]
                        ,[Lastname]
                        ,[Adress]
                        ,[DISTRICT_ID]
                        ,[AMPHUR_ID]
                        ,[PROVINCE_ID]
                        ,[Postcode]
                        ,[Phone]
                        ,[Fax]) 
                        values (@cid,@email,@pf,@fn,@ln,@ad,@sd,@d,@p,@po,@ph,@fax)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcomm.Connection = sqlcon;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@cid", GetIsNull(txtUser.Text));
            sqlcomm.Parameters.AddWithValue("@email", GetIsNull(txtE_mail.Text));
            sqlcomm.Parameters.AddWithValue("@pf", GetIsNull(DropDownList1.SelectedItem.Text));
            sqlcomm.Parameters.AddWithValue("@fn", GetIsNull(txtFName.Text));
            sqlcomm.Parameters.AddWithValue("@ln", GetIsNull(txtLname.Text));
            sqlcomm.Parameters.AddWithValue("@ad", GetIsNull(txtAdress.Text));
            sqlcomm.Parameters.AddWithValue("@sd", GetIsNull(ddlDistrict.SelectedValue));
            sqlcomm.Parameters.AddWithValue("@d", GetIsNull(ddlAmphur.SelectedValue));
            sqlcomm.Parameters.AddWithValue("@p", GetIsNull(ddlProvince.SelectedValue));
            sqlcomm.Parameters.AddWithValue("@po", GetIsNull(txtPostcode.Text));
            sqlcomm.Parameters.AddWithValue("@ph", GetIsNull(txtPhone.Text));
            sqlcomm.Parameters.AddWithValue("@fax", GetIsNull(txtFax.Text));
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            bool chk = CheckID(txtUser.Text);
            if (chk == true)
            {
                Label1.Text = "ใช้ไม่ได้";
                Label1.Enabled = false;
            }
            else
            {
                MembershipUser user = Membership.CreateUser(txtUser.Text, txtPassword.Text);
                string[] rolesArray = Roles.GetAllRoles();
                Roles.AddUserToRole(user.UserName, rolesArray[1]);
                InsertCustomer();
                Response.Redirect(Request.QueryString["ReturnUrl"] ?? "~/Account/RegisterSuccess.aspx");
            }
            

        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlAmphur.Items.Clear();
            ddlDistrict.Items.Clear();
        }

        protected void ddlAmphur_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDistrict.Items.Clear();
        }

        /*e.Command.Parameters["@Customer_ID"].Value = txtUser.Text;
            e.Command.Parameters["@E_mail"].Value = txtE_mail.Text;
            e.Command.Parameters["@Prefix"].Value = DropDownList1.SelectedItem.Text;
            e.Command.Parameters["@Firstname"].Value = txtFName.Text;
            e.Command.Parameters["@Lastname"].Value = txtLname.Text;
            e.Command.Parameters["@Adress"].Value = txtAdress.Text;
            e.Command.Parameters["@SubDistrict"].Value = txtSubDistrict.Text;
            e.Command.Parameters["@District"].Value = txtDistrict.Text;
            e.Command.Parameters["@Province"].Value = txtProvince.Text;
            e.Command.Parameters["@Postcode"].Value = txtPostcode.Text;
            e.Command.Parameters["@Phone"].Value = txtPhone.Text;
            e.Command.Parameters["@Fax"].Value = txtFax.Text;*/
    }
}